<template>
  <div>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" size="medium">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="巡检日期" prop="inspectDate" required>
            <el-date-picker
              v-model="ruleForm.inspectDate"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
              :picker-options="pickerOptions"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="IRI" prop="IRI">
            <el-input v-model="ruleForm.IRI" required />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="备注" prop="remarks">
        <el-input v-model="ruleForm.remarks" type="textarea" clearable />
      </el-form-item>

      <el-row type="flex" class="row-bg" justify="end">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </el-row>

    </el-form>
  </div>
</template>

<script>
import { addPavementFlatness, updatePavementFlatness } from '@/api/regularInspection'

export default {
  props: {
    ruleForm: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    const iriCheck = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('IRI不能为空'))
      } else if (isNaN(value)) {
        return callback(new Error('IRI必须为数值'))
      } else {
        callback()
      }
    }
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      rules: {
        IRI: [
          { required: true, validator: iriCheck }
        ],
        inspectDate: [
          { required: true, message: '巡检日期是必填项' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // bug 时间虽然看起来一样，但父组件拿过来的，展示的是字符串形式的，而不是对象
          const mm = this.ruleForm.inspectDate.getMonth() + 1
          const dd = this.ruleForm.inspectDate.getDate()
          console.log(this.ruleForm.flatnessId == null)
          if (this.ruleForm.flatnessId == null) {
            addPavementFlatness({
              userID: this.ruleForm.userId,
              IRI: this.ruleForm.IRI,
              regularInspectionID: this.ruleForm.regularInspectionID,
              // flatnessDate: ymd.Y+'-'+ymd.M+'-'+ymd.d+" 00:00:00",
              flatnessDate: [this.ruleForm.inspectDate.getFullYear(),
                (mm > 9 ? '' : '0') + mm,
                (dd > 9 ? '' : '0') + dd
              ].join('-') + ' 00:00',
              remarks: this.remarks
            }).then(() => {
              this.$message.success('添加成功')
              this.$emit('close-dialog')
              this.$emit('flatness-submited')
            })
          } else {
            updatePavementFlatness({
              flatnessID: this.ruleForm.flatnessId,
              userID: this.ruleForm.userId,
              IRI: this.ruleForm.IRI,
              regularInspectionID: this.ruleForm.regularInspectionID,
              // flatnessDate: ymd.Y+'-'+ymd.M+'-'+ymd.d+" 00:00:00",
              flatnessDate: [this.ruleForm.inspectDate.getFullYear(),
                (mm > 9 ? '' : '0') + mm,
                (dd > 9 ? '' : '0') + dd
              ].join('-') + ' 00:00',
              remarks: this.remarks
            }).then(() => {
              this.$message.success('修改成功!')
              this.$emit('close-dialog')
              this.$emit('flatness-submited')
            })
          }
        } else {
          return false
        }
      })
    },
    closeDialog() {
      this.$emit('close-dialog')
    }
  }
}
</script>
